import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

public class mainUI extends JFrame {

	private JPanel contentPane;
	private JTextField text_path;
	private JComboBox comboBox = new JComboBox<String>();
	private JTextField text_newname;


//	static {
//		try {
//			XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
//			InputStream in=factory.getClass().getResourceAsStream("/META-INF/services/org.xmlpull.v1.XmlPullParserFactory");
//			System.out.println(in);
//			System.out.println(in != null);
//			InputStreamReader isr = new InputStreamReader(in);
//			BufferedReader br = new BufferedReader(isr);
//			String line= null;
//			while((line=br.readLine())!=null){
//				System.out.println(line);
//			}
//		} catch ( XmlPullParserException | IOException e ) {
//			e.printStackTrace();
//		}
//	}

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					mainUI frame = new mainUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public mainUI() {
        setTitle("epub字体转换");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 626, 412);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		text_path = new JTextField();
		text_path.setText("D:\\娱乐\\12\\镰池和马 - 創約·魔法禁書目錄4.epub");
		text_path.setBounds(153, 41, 187, 21);
		contentPane.add(text_path);
		text_path.setColumns(10);

		JLabel lblNewLabel = new JLabel("文件路径选择：");
		lblNewLabel.setBounds(62, 38, 99, 26);
		contentPane.add(lblNewLabel);

		JButton btnNewButton = new JButton("...");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					JFileChooser chooser1 = new JFileChooser();
					int flag = chooser1.showOpenDialog(contentPane);
					text_path.setText(chooser1.getSelectedFile().getAbsolutePath());
				}catch(Exception e) {
					//e.printStackTrace();
				}
			}
		});
		btnNewButton.setBounds(350, 40, 24, 23);
		contentPane.add(btnNewButton);

		final JTextArea textArea = new JTextArea();
		textArea.setBounds(43, 106, 533, 257);
		textArea.setLineWrap(true);        //激活自动换行功能
		textArea.setWrapStyleWord(true);   //激活断行不断字功能
		contentPane.add(textArea);

		JButton btnNewButton_1 = new JButton("开始转换");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String path=text_path.getText().trim();
				path= path.replaceAll("\\\\", "\\\\\\\\");
				//System.out.println(path);
				if(path.equals("")) {
					JOptionPane.showMessageDialog(contentPane, "未选择文件");
					return;
				}

				String name = path.substring(path.lastIndexOf(".")+1);
				if(!name.equalsIgnoreCase("epub")){
					JOptionPane.showMessageDialog(contentPane, "选择的文件格式不为epub,请重新选择文件");
					return;
				}

				String newname=text_newname.getText().trim();
				if(!isEpub(newname)){
					JOptionPane.showMessageDialog(contentPane, "新文件的格式不为epub,请重新选择文件名");
					return;
				}

				textArea.append(path+"开始转换\n");
				Date start=new Date();
				String change=comboBox.getSelectedItem()+"";
				boolean flag=change.equals("繁转简")?false:true;
				//System.out.println(path+":"+change+":"+flag);
				textArea.append(path+product.file(path,flag,newname));
				Date end=new Date();
				int m=end.compareTo(start);
				textArea.append("\t共耗时："+m+"毫秒\n");
			}
		});
		btnNewButton_1.setBounds(469, 40, 93, 23);
		contentPane.add(btnNewButton_1);

		comboBox.addItem("繁转简");
		comboBox.addItem("简转繁");
		comboBox.setToolTipText("");
		comboBox.setBounds(384, 41, 75, 21);
		contentPane.add(comboBox);

		JLabel lblNewLabel_1 = new JLabel("新文件名：");
		lblNewLabel_1.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNewLabel_1.setBounds(72, 74, 75, 15);
		contentPane.add(lblNewLabel_1);

		text_newname = new JTextField();
		text_newname.setText("newfile.epub");
		text_newname.setBounds(153, 72, 187, 21);
		contentPane.add(text_newname);
		text_newname.setColumns(10);

		mainUI.this.setLocationRelativeTo(null);
	}
	public boolean isEpub(String path){
		String newname=path;
		if(newname.substring(newname.lastIndexOf(".")+1).equalsIgnoreCase("epub")){
			return true;
		}else{
			return false;
		}
	}
}
